home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / STRX.ZIP / TOKENS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-05  |  2.1 KB  |  87 lines

  1. //
  2. // tokens.cpp : tokens class implementation
  3. // Author     : Roy S. Woll
  4. //
  5. // Copyright (c) 1993 by Roy S. Woll
  6. // You may distribute this source freely as long as you leave all files
  7. // in their original form, including the copyright notice as is.
  8. //
  9. //
  10. // Version 1.00     2/1/93
  11. //
  12. //  Implementation file for class tokens.  Tokens will break up
  13. //  a character buffer into one or more tokens, where the delimeters are 
  14. //  specified by the user.
  15. //
  16.  
  17. #include <tokens.h>
  18. #include <str.h>
  19. #include <minmax.h>
  20. #include <string.h>
  21.  
  22.    int tokens::_gettoken(int& pos){
  23.       if ((*curtoken)==0) return 0;
  24.       char temp;
  25.  
  26.       int delimeters = strspn(curtoken, delimptr); 
  27.       if (skipRepeatingDelimeters) curtoken += delimeters;
  28.       else if (delimeters) if (!firstTime) curtoken++;
  29.  
  30.       firstTime = 0;
  31.  
  32.       if (endingpos) {// Force strcspn to not exceed endingpos
  33.          if ((curtoken-alltokens)>endingpos) return 0;
  34.          temp = alltokens[endingpos+1];
  35.          alltokens[endingpos+1]=0;
  36.       };
  37.  
  38.       pos = strcspn(curtoken, delimptr);
  39.  
  40.       if (endingpos) {alltokens[endingpos+1]=temp;};
  41.       return 1;
  42.    };
  43.  
  44.  
  45. //
  46. // Create a token parser
  47. //     break p_tokens into tokens defined by a delimeter set p_delim
  48. //     Only search up to p_endingpos
  49. //
  50.    tokens::tokens(const char * p_tokens,
  51.           const char * p_delim, int p_endingpos){
  52.       delimptr = (char *)p_delim;
  53.       curtoken = alltokens = (char *)p_tokens;
  54.       endingpos = p_endingpos;
  55.       skipRepeatingDelimeters = 1;
  56.       firstTime = 1;
  57.    };
  58.  
  59.  
  60.    int tokens::getToken(char * word, int maxlen){
  61.       int pos;
  62.       if (_gettoken(pos)) {
  63.                         pos = min(pos, maxlen);
  64.          strncpy(word, curtoken, pos);
  65.          word[pos]=0;
  66.          curtoken += pos;
  67.  
  68.          return 1;
  69.  
  70.       }
  71.       else return 0;
  72.    };
  73.  
  74.    int tokens::getToken(str * word){
  75.       int pos;
  76.       if (_gettoken(pos)) {
  77.          word->assign(curtoken, pos); // assign word
  78.          curtoken += pos;
  79.  
  80.          return 1;
  81.       }
  82.       else return 0;
  83.    };
  84.  
  85.  
  86.  
  87.